﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BILLManager.Library.Utils;

namespace BILLManager.Library.DAL
{
    public class UserDAL : IDisposable
    {

        private dbBillsEntities entities;

        #region INSERT/UPDATE/DELETE

        public int AddUser(tbUser user)
        {
            if (entities == null) entities = new dbBillsEntities();

            try
            {

            }
            catch (Exception ex)
            {
                throw ex;
            }

            user.USER_PASS = this.EncryptPassword(user.USER_PASS);
            user.USER_DT_UPDATED = DateTime.Now;
            entities.AddTotbUser(user);
            entities.SaveChanges();
            return user.USER_ID;
            
        }

        #endregion

        #region Utilities Methods

        private string EncryptPassword(string pass)
        {
            Crypto crypter = new Crypto();
            string ret = crypter.Encrypt(pass);
            crypter = null;
            return ret;
        }

        #endregion

        #region Implement IDisposable

        // Basic implementation of IDisposable.
        // This ensures that ObjectContext is disposed of correctly.

        /// <summary>
        /// Track whether Dispose has been called
        /// </summary>
        private bool disposed;

        /// <summary>
        /// Implement Dispose from IDisposable
        /// </summary>
        void IDisposable.Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Do the Dispose logic
        /// </summary>
        /// <param name="disposing">true if called from Dispose, false if called from finalizer</param>
        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (entities != null) entities.Dispose();
                }
                disposed = true;
            }
        }

        /// <summary>
		/// Finalizer
		/// </summary>
        ~UserDAL()
		{
			Dispose(false);
		}

        #endregion

        
    }
}
